<?php

include_once(APP_URL.'protected/myUtil/CheckUtil.php');
class AppController extends Controller {

    public function actionPostFback() {
        // http://112.74.203.16/wifishare_server/index.php?r=app/postfback
    
        // check param
        $userId = CheckUtil::checkPostParam(112201, 'userId', false);
        $userSession = CheckUtil::checkPostParam(112201, 'session', false);
        $fbackContent = CheckUtil::checkPostParam(101201, 'feedbackContent', false);
        $fbackPlatform = CheckUtil::checkPostParam(101201, 'appPlatform', false);
        $fbackTime = CheckUtil::checkPostParam(101201, 'time', false);
        $fbackVerscode = CheckUtil::checkPostParam(101201, 'appVersionCode', false);
    
        //identify user
        $result = $this->userIdentify($userId, $userSession);
        if (!$result) {
            echo EchoUtil::retFailed(112201, 'user not exsit !');
            exit;
        }
    
        // add record
        $TblFback = new TblFback();
        $contentArr = array(
            'fback_uid' => $userId,
            'fback_content' => $fbackContent,
            'fback_platform' => $fbackPlatform,
            'fback_time' => $fbackTime,
            'fback_verscode' => $fbackVerscode
        );// fill content
        if(!$TblFback->addRecord('fback', $contentArr)) {
            echo EchoUtil::retFailed(101202, 'post fback failed !');
            exit;
        }

        // return success
        echo EchoUtil::msgSuccess(1, (object)array());
    
    }
    
    public function actionPostBcardVerify() {
        // http://112.74.203.16/wifishare_server/index.php?r=app/postbcardverify
         
        // check param
        $userId = CheckUtil::checkPostParam(111501, 'userId', false);
        $userSession = CheckUtil::checkPostParam(111501, 'session', false);
        $bcardMaster = CheckUtil::checkPostParam(111501, 'cardholderName', false);
        $bcardPhone = base64_decode(CheckUtil::checkPostParam(111501, 'cardholderPhone', false));
        $bcardNum = base64_decode(CheckUtil::checkPostParam(111501, 'accountNum', false));
        $bcardType = CheckUtil::checkPostParam(111501, 'bankType', false);
        
        //identify user
        $result = $this->userIdentify($userId, $userSession);
        if (!$result) {
            echo EchoUtil::retFailed(111501, 'user not exsit !');
            exit;
        }
        
        // third part verify completed !
        // ...
         
    
        // return success
        echo EchoUtil::msgSuccess(1, (object)array());
         
    }
    
    public function actionPostBcardType() {
        // http://112.74.203.16/wifishare_server/index.php?r=app/postcardtype
    
        // check param
        $userId = CheckUtil::checkPostParam(111401, 'userId', false);
        $userSession = CheckUtil::checkPostParam(111401, 'session', false);
        $bcardNum = CheckUtil::checkPostParam(111401, 'accountNum', false);
    
        //identify user
        $result = $this->userIdentify($userId, $userSession);
        if (!$result) {
            echo EchoUtil::retFailed(111401, 'user not exsit !');
            exit;
        }
    
        // bcard num invalid
        // bcard interface ...
        
        $result = (object)array(
            'bcard_type' => 1,
            'bcard_name' => '邮储银行河南分行'
        );
        if (!$result) {
            echo EchoUtil::retFailed(111402, 'get bcard type failed !');
            exit;
        }
    
        // return success
        echo EchoUtil::msgSuccess(1, array(
            'bankTpye' => $result->bcard_type,
            'bankName' => $result->bcard_name
        ));
    
    }
    
    public function actionGetApkg() {
        // http://112.74.203.16/wifishare_server/index.php?r=app/getapkg
         
        // check param
        $userId = CheckUtil::checkGetParam(101001, 'userId', false);
        $userSession = CheckUtil::checkGetParam(101001, 'session', false);
        
        //identify user
        $result = $this->userIdentify($userId, $userSession);
        if (!$result) {
            echo EchoUtil::retFailed(101001, 'user not exsit !');
            exit;
        }

        // no apkg
        $TblApkg = new TblApkg();
        $conditionArr = array(
            'select' => array('*'),
            'order' => 'apkg_id ASC'
        );// fill condition
        $result = $TblApkg->selectRecordAll($conditionArr);
        if (!$result) {
            echo EchoUtil::retFailed(101002, 'get apkg failed !');
            exit;
        }

        foreach ($result as $value) {
            $dataArray[] = array(
                'packageType' => $value->apkg_type,
                'packageName' => $value->apkg_name,
                'packagePrice' => $value->apkg_price,
                'packageBrief' => $value->apkg_brief,
                'packageDetail' => $value->apkg_detail
            );
        }

	    // return success
        echo EchoUtil::msgSuccess(1, array('list'=>$dataArray));
    
    }
	
	public function actionGetAvers() {
	    // http://112.74.203.16/wifishare_server/index.php?r=app/getavers

	    // param check
	    $aversPlatform = CheckUtil::checkGetParam(101201, 'appPlatform', false);
        
	    // version info is null
	    $TblAvers = new TblAvers();
	    $conditionArr = array(
	        'select' => array('*'),
	        'order' => 'avers_id DESC',
	        'limit' => 1,
	        'condition' => 'avers_platform=:aversPlatform',
	        'params' => array('aversPlatform'=>$aversPlatform)
	    );// fill condition
	    $result = $TblAvers->selectRecordAll($conditionArr);
	    if(!$result) {
            echo EchoUtil::retFailed(101202, 'get avers failed !');
            exit;
	    }

	    // return success
	    echo EchoUtil::msgSuccess(1, array(
	        'versionCode' => $result[0]->avers_code,
	        'appName' => $result[0]->avers_name,
	        'describeInfo' => $result[0]->avers_content,
	        'appSize' => $result[0]->avers_size,
	        'downloadUrl' => $result[0]->avers_durl
	    ));
	    
	}
	
	public function actionGetAgreement() {
	    // http://112.74.203.16/wifishare_server/index.php?r=app/getagreement
	    
        $this->renderPartial('agreement');
        
	}

	public function actionGetAarea() {
	    // http://112.74.203.16/wifishare_server/index.php?r=app/getaarea
	
	    // check param
	    $userId = CheckUtil::checkGetParam(101401, 'userId', false);
	    $userSession = CheckUtil::checkGetParam(101401, 'session', false);
	    $country = CheckUtil::checkGetParam(101401, 'country', false);
	    $province = CheckUtil::checkGetParam(101401, 'province', false);
	    $city = CheckUtil::checkGetParam(101401, 'city', false);
	    $area = CheckUtil::checkGetParam(101401, 'district', false);

	    //identify user
	    $result = $this->userIdentify($userId, $userSession);
	    if (!$result) {
	        echo EchoUtil::retFailed(101401, 'user not exsit !');
	        exit;
	    }
	    
// 	    echo $country,'/',$province,'/',$city,'/',$area;exit;
	    //http://112.74.203.16/wifishare_server/index.php?r=app/getaarea&session=9b046a391f0d9725b98ec1e64b174357&userId=9&country=%E4%B8%AD%E5%9B%BD&province=%E5%AE%89%E5%BE%BD%E7%9C%81&city=%E5%AE%BF%E5%B7%9E%E5%B8%82&district=%E5%9F%87%E6%A1%A5%E5%8C%BA
	    
	    $TblAprovince = new TblAprovince();
	    $conditionArr = array(
	        'select' => array('province_id'),
	        'condition' => 'province=:province',
	        'params' => array('province'=>$province)
	    );// fill condition
	    $result = $TblAprovince->selectRecord($conditionArr);
	    if(!$result) {
	        echo EchoUtil::retFailed(101402, 'get area id failed !');
	        exit;
	    }
	    $provinceId = $result->province_id;

	    $TblAcity = new TblAcity();
	    $conditionArr = array(
	        'select' => array('city_id'),
	        'condition' => 'city=:city and province_id=:provinceId',
	        'params' => array('city'=>$city, 'provinceId'=>$provinceId)
	    );// fill condition
	    $result = $TblAcity->selectRecord($conditionArr);
	    if(!$result) {
	        echo EchoUtil::retFailed(101402, 'get area id failed !');
	        exit;
	    }
	    $cityId = $result->city_id;
	    
	    $TblAarea = new TblAarea();
	    $conditionArr = array(
	        'select' => array('area_id'),
	        'condition' => 'area=:area and city_id=:cityId',
	        'params' => array('area'=>$area, 'cityId'=>$cityId)
	    );// fill condition
	    $result = $TblAarea->selectRecord($conditionArr);
	    if(!$result) {
	        echo EchoUtil::retFailed(101402, 'get area id failed !');
	        exit;
	    }
	    $areaId = $result->area_id;

	    if (!$areaId) {
	        echo EchoUtil::retFailed(101402, 'get area id failed !');
	        exit;
	    }
	
	    // return success
	    echo EchoUtil::retSucc(1, array(
	        'areaId' => $areaId
	    ));
	     
	}
	
}
